package study.saas.web.portal.sys.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.order.entity.Commodity;
import study.saas.order.service.CommodityFacade;
import study.saas.order.service.model.*;

/**
 * @author zxx
 */

@Api(tags = "商品管理")
@RestController
@RequestMapping(value = "sys/commodity")
public class CommodityController {

    @Reference(version = "1.0.0")
    private CommodityFacade commodityFacade;

    @ApiOperation(value = "获取")
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public ApiResultV2<Commodity> get(String commodityName) {
        if(commodityName==null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.get(commodityName);
    }
    @ApiOperation(value = "创建")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResultV2 add(CommodityAddDTO add) {
        if (add == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.addCommodity(add);
    }

    @ApiOperation(value = "修改")
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public ApiResultV2 modify(CommodityModifyDTO modify) {
        if (modify == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.modifyCommodity(modify);
    }

    @ApiOperation(value = "商品下架")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ApiResultV2 delete(String commodityName) {
        if (commodityName == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.deleteCommodity(commodityName);
    }

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResultV2 list(CommodityFilterDTO filter) {
        if (filter == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.listCommodity(filter);
    }

    @ApiOperation(value = "商品上架")
    @RequestMapping(value = "/up", method = RequestMethod.POST)
    public ApiResultV2 up(String commodityName) {
        if (commodityName == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.upCommodity(commodityName);
    }

    @ApiOperation(value = "库存减少")
    @RequestMapping(value = "/decreaseInventory", method = RequestMethod.POST)
    public ApiResultV2 decreaseInventory(CommodityDecreaseDTO decrease) {
        if (decrease == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.decreaseCommodity(decrease);
    }

    @ApiOperation(value = "库存增加")
    @RequestMapping(value = "/increaseInventory", method = RequestMethod.POST)
    public ApiResultV2 increaseInventory(CommodityIncreaseDTO increase) {
        if (increase == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.increaseCommodity(increase);
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public ApiResultV2 remove(String commodityName) {
        if (commodityName == null) {
            return ApiResultV2.error("商品名字不能为空！");
        }
        return commodityFacade.removeCommodity(commodityName);
    }
}
